package com.iflytek.universitymanagement.controller;

import com.iflytek.universitymanagement.domain.Question;
import com.iflytek.universitymanagement.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping("/api/questions")
public class QuestionController {

    private static final Logger logger = Logger.getLogger(QuestionController.class.getName());

    private final QuestionService questionService;

    @Autowired
    public QuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    /**
     * 获取所有心理健康测试题目
     *
     * @return JSON 格式的题目列表
     */
    @GetMapping
    public Map<String, Object> getAllQuestions() {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Question> questions = questionService.list();  // 获取题目列表
            if (questions.isEmpty()) {
                response.put("code", 404);
                response.put("message", "没有找到题目数据");
                logger.warning("没有找到题目数据");
            } else {
                response.put("code", 200);
                response.put("message", "success");
                response.put("items", questions);

                // 输出数据库中的题目信息到控制台
                logger.info("成功返回题目数据: " + questions.size() + "条记录");

                // 逐个输出每个题目的详细信息
                for (Question question : questions) {
                    logger.info("题目ID: " + question.getTid() + ", 题目内容: " + question.getTExplain());
                }
            }
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "服务器内部错误");
            logger.severe("获取题目时发生错误: " + e.getMessage());
        }
        return response;
    }

    /**
     * 提交用户答题结果并计算总分
     *
     * @param answers 用户提交的答题结果
     * @return JSON 格式的总分结果
     */
    @PostMapping("/submit")
    public Map<String, Object> submitAnswers(@RequestBody List<AnswerRequest> answers) {
        Map<String, Object> response = new HashMap<>();
        try {
            int totalScore = questionService.calculateTotalScore(answers);  // 计算总分
            response.put("code", 200);
            response.put("message", "success");
            response.put("totalScore", totalScore);
            logger.info("用户答题完成，总分: " + totalScore);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "服务器内部错误");
            logger.severe("提交答案时发生错误: " + e.getMessage());
        }
        return response;
    }

    /**
     * 用户答题请求类，用于接收用户提交的答案
     */
    public static class AnswerRequest {
        private Integer tid; // 题目 ID
        private Integer selectedAnswer; // 用户选择的答案

        public Integer getTid() {
            return tid;
        }

        public void setTid(Integer tid) {
            this.tid = tid;
        }

        public Integer getSelectedAnswer() {
            return selectedAnswer;
        }

        public void setSelectedAnswer(Integer selectedAnswer) {
            this.selectedAnswer = selectedAnswer;
        }
    }
}
